package com.example.admin.smstest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.admin.smstest.permission.CheckPermissionsActivity;
import com.example.admin.smstest.sms.SmsActivity;
import com.example.admin.smstest.utils.ByteUtil;
import com.example.admin.smstest.utils.CrcOperateUtil;
import com.example.admin.smstest.utils.Hex;
import com.example.admin.smstest.utils.SharedPreferencesUtils;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class MainActivity extends CheckPermissionsActivity {

    private Button sendButton;
    private Button smsButton;
    private TextView textView;
    private TextView connect;
    private final int RESULT_CODE_BTDEVICE = 0;

    //private ConnectionManager mConnectionManager;//链接监听类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendButton = (Button) findViewById(R.id.send_button);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点击发送了",Toast.LENGTH_SHORT).show();
//                try {
//                    //sendMessage();
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                }
            }
        });
        smsButton = (Button) findViewById(R.id.sms_button);
        smsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.startActivity(new Intent(MainActivity.this, SmsActivity.class));
            }
        });
        textView = (TextView) findViewById(R.id.text);
        connect = (TextView) findViewById(R.id.connect);
        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, DeviceActivity.class);
                startActivityForResult(i, RESULT_CODE_BTDEVICE);
            }
        });
        SharedPreferencesUtils sp = SharedPreferencesUtils.getInstance(MainActivity.this,"sms_data");
        sp.put("device_address","");
        //mConnectionManager = new ConnectionManager(mConnectionListener);
        //mConnectionManager.startListen();
    }

    /**
     * 链接监听
     */
//    private ConnectionManager.ConnectionListener mConnectionListener = new ConnectionManager.ConnectionListener() {
//
//        @Override
//        public void onConnectStateChange(int oldState, int State) {
//        }
//
//        @Override
//        public void onListenStateChange(int oldState, int State) {
//        }
//
//        @Override
//        public void onSendData(boolean suc, byte[] data) throws UnsupportedEncodingException {
//            System.out.println("发送："+Arrays.toString(data));
//            //getData(data);
//        }
//
//        @Override
//        public void onReadData(byte[] data) {
//            System.out.println("接收："+Arrays.toString(data));
//            //getData(data);
//        }
//    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("mainactivity", "onActivityResult, requestCode="+requestCode+" resultCode="+resultCode );
        if(requestCode == RESULT_CODE_BTDEVICE && resultCode == RESULT_OK) {
            String deviceAddr = data.getStringExtra("DEVICE_ADDR");
            //mConnectionManager.connect(deviceAddr);
        }
    }
    private void sendMessage() throws UnsupportedEncodingException {
        //  String content = mMessageEditor.getText().toString();
        //  byte[] contentb = content.getBytes();

        // value开始 2017年5月31日10时31分50秒
        byte[] smsValueB = null;
        //457ea7f2
        byte[] timeB = { (byte)0xf2, (byte)0xa7,0x7e,0x45 };
        byte[] idB = {0x01,0x01};
        byte[] numberB = null;
        String _id = null;
        String address = null;
        String body = null;
        _id = "999";
        System.out.println(Arrays.toString(timeB));
        address = "维信诺电话";
        //短信的电话号码------------------------------------
        byte[] numberC= address.getBytes("UNICODE");//多出两个字节 -1 -2
        numberB = ByteUtil.cutBytes(numberC,2,numberC.length-2);
        byte[] numberLengthB = ByteUtil.intToHexBytes(numberB.length);
        System.out.println("短信电话长度2==="+Integer.toString(numberB.length));
        System.out.println("名字的长度"+Arrays.toString(numberLengthB));
        byte[] destinationB = {0x01};

        //获取当前的短信内容------------------------------------
        body = "你啥的发的顺";
        byte[] bodyByte1 = body.getBytes("UNICODE");
        byte[] bodyByte= ByteUtil.cutBytes(body.getBytes("UNICODE"),2,bodyByte1.length-2);
        byte[] bodyByteLength = ByteUtil.intToHexBytes(bodyByte.length);

        smsValueB = ByteUtil.concatAll(timeB,destinationB,idB,numberLengthB,numberB,bodyByteLength,bodyByte);
        System.out.println("value---------"+Arrays.toString(smsValueB));
        // 1.帧头
        byte[] frameHeader = { (byte) 0xA5, (byte) 0xA5, (byte) 0xA5,
                (byte) 0xA5};// 4B
        byte[] function = {(byte) 0xAA };
        // 短信长度
        byte[] smsByteLength = Hex.hexStr2Bytes(Integer.toHexString(smsValueB.length));
        System.out.println("短信长度==="+Arrays.toString(smsByteLength));
        // TLV长度
        byte[] valueTotalLength = Hex.hexStr2Bytes(Integer.toHexString(smsValueB.length+7));
        System.out.println("短信长度==="+Arrays.toString(valueTotalLength));
        //                   | 总包数   |  当前包数 |序列号|业务号|操作类型 (type)| Type(TLV)
        byte[] baowentou = { 0x01, 0x00, 0x01, 0x00, 0x01,  0x05,        0x03,        0x01};// 10B
        // 加crc校验
        byte[] andData = ByteUtil
                .concatAll(function,valueTotalLength , baowentou,smsByteLength, smsValueB);
        System.out.println("加密前"+Arrays.toString(andData));
        byte[] endData = CrcOperateUtil.setParamCRC(andData);
        System.out.println("加密后"+Arrays.toString(endData));
        byte[] data = ByteUtil.concatAll(frameHeader, endData);
        System.out.println("完整加密后"+Arrays.toString(data));
        System.out.println("长度=="+data.length);
//        for ( int i = 0; i < data.length; i++){
//            System.out.println(i+"====="+Integer.toHexString(data[i]));
//        }
        //mConnectionManager.sendData(data);
    }

}
